package com.icinema.main;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.datepicker.client.DateBox;
import com.icinema.client.CinemaService;
import com.icinema.client.CinemaServiceAsync;
import com.icinema.client.ICinema;
import com.icinema.client.MovieService;
import com.icinema.client.MovieServiceAsync;
import com.icinema.vo.Cinema;
import com.icinema.vo.MovieDetail;
import com.icinema.vo.MovieShowing;
import com.icinema.vo.MovieShowingDetail;

public class MoviesListView extends Composite {
	ICinema iCinema;
	DateBox fromDate = new DateBox();
	DateBox toDate = new DateBox();
	ListBox lstCinemas = new ListBox();
	ListBox lstMovies = new ListBox();
	
	private final MovieServiceAsync movieService = GWT
	.create(MovieService.class);
	
	private final CinemaServiceAsync cinemaService = GWT
	.create(CinemaService.class);	
	
	FlexTable rows = new FlexTable();
	
	public MoviesListView(){
		VerticalPanel panel = new VerticalPanel();
		initWidget(panel);
		
		panel.add(rows);
		
		FlexTable formTable = new FlexTable();
		formTable.setText(0,0,"Cinema");		
		formTable.setText(0,1,"Movie");
		formTable.setText(0,2,"Showing From Date");
		formTable.setText(0,3,"Showing To Date");
		formTable.getRowFormatter().setStyleName(0, "tableViewBrowseHeaders");
		formTable.setWidget(1,0,lstCinemas);
		formTable.setWidget(1,1,lstMovies);
		formTable.setWidget(1,2,fromDate);
		formTable.setWidget(1,3, toDate);
		Button addButton = new Button("Add");
		addButton.addMouseUpHandler(new MouseUpHandler(){

			@Override
			public void onMouseUp(MouseUpEvent event) {
				MovieShowing movieShowing = new MovieShowing();
				movieShowing.setCinemaId(lstCinemas.getValue(lstCinemas.getSelectedIndex()));
				movieShowing.setMovieId(lstMovies.getValue(lstMovies.getSelectedIndex()));
				movieShowing.setShowingFromDate(fromDate.getTextBox().getText());
				movieShowing.setShowingToDate(toDate.getTextBox().getText());
				movieService.addMovieShowing(movieShowing, new AsyncCallback<String>(){
					@Override
					public void onFailure(Throwable caught) {
				
						
					}

					@Override
					public void onSuccess(String result) {
						fromDate.getTextBox().setText("");
						toDate.getTextBox().setText("");
						refresh();
					}
					
				});
			}
			
		});
		formTable.setWidget(1,4, addButton);
		panel.add(formTable);
		
	}
	
	public void loadShowList(){
			rows.clear();
			rows.clear(true);
			
			movieService.getMovieShowings(new AsyncCallback<List<MovieShowingDetail>>(){

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<MovieShowingDetail> result) {
				rows.removeAllRows();
				rows.getRowFormatter().setStyleName(0, "tableViewBrowseHeaders");
				rows.setText(0,0,"Cinema");
				rows.setText(0,1,"Movie");
				rows.setText(0,2,"Showing From Date");
				rows.setText(0,3,"Showing To Date");
				CellFormatter formatter = rows.getCellFormatter();
				for(int i=0; i < result.size();i++ ){
						formatter.setStyleName(i+1,0,"tableCell");
						formatter.setStyleName(i+1,1,"tableCell");
						formatter.setStyleName(i+1,2,"tableCell");
						formatter.setStyleName(i+1,3,"tableCell");
						Image imgDelete = new Image("/images/delete.png");
						imgDelete.setStyleName("imgButton");
						String showId = result.get(i).getMovieShowing().getId();
						imgDelete.addMouseDownHandler(new ImageDeleteMouseDownHandler(showId));
						rows.setText(i+1,0,result.get(i).getCinema().getName());	
						rows.setText(i+1,1, result.get(i).getMovie().getName());
						rows.setText(i+1,2, result.get(i).getMovieShowing().getShowingFromDate());
						rows.setText(i+1,3, result.get(i).getMovieShowing().getShowingToDate());
						rows.setWidget(i+1,4, imgDelete);
				}				
			}
			
		});
	}
	class ImageDeleteMouseDownHandler implements  MouseDownHandler{
		private String movieShowingId; 
		public ImageDeleteMouseDownHandler(String movieShowingId){
			this.movieShowingId = movieShowingId;
		}
		@Override
		public void onMouseDown(MouseDownEvent event) {
			if(!Window.confirm("This will Delete the Show Time")){
				return;
			}
			movieService.deleteShowingMovie(movieShowingId, new AsyncCallback<Void>(){

				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(Void result) {
				}
			});
			refresh();
		}
	}	
	public void loadMovieList(){
		movieService.getMovies(new AsyncCallback<List<MovieDetail>>(){

			@Override
			public void onFailure(Throwable caught) {
			
				
			}

			@Override
			public void onSuccess(List<MovieDetail> result) {
				lstMovies.clear();
				for(MovieDetail movie:result){
					lstMovies.addItem(movie.getMovie().getName(),movie.getMovie().getId());
				}
			}
		});
	}
	
	public void loadCinemaList(){
		cinemaService.getCinemas(new AsyncCallback<List<Cinema>>(){

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(List<Cinema> result) {
				lstCinemas.clear();
				for(Cinema cinema:result){
					lstCinemas.addItem(cinema.getName(),cinema.getId());
				}
			}
			
		});
	}
	
	public void refresh(){
		this.setVisible(false);
		this.setVisible(true);
	}	
	
	@Override
	public void setVisible(boolean visible) {
		if(visible == true){
			loadMovieList();
			loadCinemaList();
			loadShowList();
		}
		super.setVisible(visible);
	}

	public void setICinema(ICinema icCinema){
		this.iCinema = icCinema;
	}
	public class CinemaMouseDownHandler implements MouseDownHandler{
		String cinemaId;
		ICinema c;
		public CinemaMouseDownHandler(String cinema){
			cinemaId = cinema;
		}
		
		@Override
		public void onMouseDown(MouseDownEvent event) {
			Window.alert("Currently Click on "+ cinemaId);
			iCinema.executeAddCinemaViewCommand();
		}
	}
}
